<template>
  <div id="space-settings-general">
    <el-form ref="spaceSettingsGeneralForm" label-position="top" class="bold-label form-border"
             :model="form" :rules="rules">
      <el-form-item label="空间名" prop="title" ref="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="空间描述">
        <el-input v-model="form.desc" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="空间封面">
        <div class="flex">
          <el-avatar :size="64" :src="previewAvatar" shape="square"/>
          <div class="body-line-height m-l-16">
            <div class="flex">
              <UploadAvatar :preview-url.sync="form.avatar" v-on:avatar="setAvatar" />
              <el-button size="small" class="m-l-10" @click="setAvatar({})">重置</el-button>
            </div>
            <p class="info">仅支持 jpg, jpeg, png 格式的图片类型。<br>图片允许的最大大小为2MB。</p>
          </div>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit" :loading="isLoading">保存修改</el-button>
        <el-button class="right" @click="$emit('update:activeItem', '')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import UploadAvatar from '@/components/shared/UploadAvatar';
import * as format from '@/components/spaces/format';

export default {
  name: 'General', // 常规设置
  components: { UploadAvatar },
  props: {
    activeItem: { // 当前激活的类型
      type: String,
      required: true,
    },
  },
  data() {
    return {
      form: JSON.parse(JSON.stringify(this.$store.state.currentSpace)),
      rules: {
        title: [{ required: true, validator: this.$va.spaceTitle, trigger: 'blur' }],
      },
      isLoading: false, // 正在修改
    };
  },
  computed: {
    previewAvatar() { return this.form.avatar || this.$store.state.currentSpace.avatar; },
    initUpdateParams() {
      return {
        spaceId: this.$route.params.spaceId,
        title: this.form.title,
        desc: this.form.desc,
        avatarKey: this.form.avatarKey,
      };
    },
  },
  methods: {
    // 确认修改
    onSubmit() {
      this.$refs.spaceSettingsGeneralForm.validate((valid, errs) => {
        if (valid) {
          this.isLoading = true;
          this.$api.spaces.update(this.initUpdateParams)
            .then((res) => {
              this.$message.success('修改成功');
              this.$store.dispatch('setCurrentSpace', format.formatSpace(res.data));
              this.resetForm(); // 重置表单，不然封面上传会出问题
            })
            .catch(() => {})
            .then(() => { this.isLoading = false; });
        } else this.$refs[Object.keys(errs)[0]].$el.scrollIntoView({ block: 'center' });
      });
    },
    // 重置表单（更新成功时需要更新当前页面的form）
    resetForm() { this.form = JSON.parse(JSON.stringify(this.$store.state.currentSpace)); },
    // 接受上传组件返回的头像数据
    setAvatar(avatar) {
      this.form.avatar = avatar.url || this.$store.state.currentSpace.avatar;
      this.form.avatarKey = avatar.key;
    },
  },
  watch: {
    activeItem() { // 激活时重置表单
      if (this.activeItem === this.$C.spaces.settings.general) this.resetForm();
    },
  },
};
</script>

<style></style>
